package com.gqr.threadsafe;

/**
 * Copyright (C), 2020-2024,gqr
 * FileName:MultiThreadError5
 * author:guqingrong
 * Date:2024-04-27 10:50
 * Description:观察者模式
 * History:
 * <author>    <time>    <version>    <desc>
 * 作者姓名     修改时间     版本号        描述
 */
public class MultiThreadError5 {

    int count;
    public MultiThreadError5(MySource source){
        source.registerListener(new EventListener() {
            @Override
            public void onEvent(Event e) {
                System.out.println("\n我得到的数字是"+count);
            }
        });

        for (int i = 0; i < 10000; i++) {
            System.out.print(i);
        }
        count=100;
    }

    public static void main(String[] args) {
        MySource mySource = new MySource();
        new Thread(new Runnable(){

            @Override
            public void run() {
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                mySource.eventCome(new Event() {});
            }
        }).start();
        MultiThreadError5 error5 = new MultiThreadError5(mySource);
    }


    static class MySource {

        private EventListener listener;

        void registerListener(EventListener eventListener) {
            this.listener = eventListener;
        }

        void eventCome(Event e) {
            if (listener != null) {
                listener.onEvent(e);
            } else {
                System.out.println("还未初始化完毕");
            }
        }

    }

    interface EventListener {

        void onEvent(Event e);
    }

    interface Event {

    }
}
